package TAIC.Classifier;

public class Cosine extends Divergence {

	@Override
	public double cale(double[] x, double[] y) {
		double x2 = 0 , y2 = 0 , xy = 0 ;
		int min = Math.min ( x.length , y.length ); 
		for ( int i = 0 ; i < min ; i ++  ) {
			x2 += x[ i ] * x [ i ] ;
			y2 += y[ i ] * y [ i ] ;
			xy += x [ i ] * y [ i ] ;
		}
		return - xy * xy  / x2 / y2 ;
	}
	
	public void normalize ( double x [] ) {
		double xx = 0.0 ; 
		for ( int i = 0 ; i < x.length ; i ++ ) xx += x[ i ] * x [ i ] 	;
		xx = Math.sqrt ( xx ) ; 
		for ( int i = 0 ; i < x.length ; i ++ ) x [ i ] /= xx ; 
	}

}
